package com.newworld.dmsapp2.ui.view;

import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.newworld.dmsapp2.R;
import com.newworld.dmsapp2.fragment.bean.RelationType;
import com.newworld.dmsapp2.ui.view.CustomEditSexDialog.OnStateChagedListener;

public class CustomAlertDialog {
	private TextView title;
	private RadioGroup radioGroup_relation;
	private RadioButton radioButton_family, radioButton_relation,
			radioButton_rentant;
	private Button cancel;
	private Button ensure;
	private Dialog mDialog;
	private String str;
	private LayoutInflater mInflater;
	private OnStateChagedListener mCallBack;
	private Context context;

	public CustomAlertDialog(Context context) {
		this.context = context;
		mDialog = new Dialog(context, R.style.customDialog); // 一个无title样式
		mInflater = LayoutInflater.from(context);

		View dialogView = mInflater.inflate(R.layout.custom_alertdialog, null);

		Window window = mDialog.getWindow();
		window.setContentView(dialogView);

		findView(window);
	}

	private void findView(Window window) {
		title = (TextView) window.findViewById(R.id.custom_alertdialog_title);
		radioGroup_relation = (RadioGroup) window
				.findViewById(R.id.radioGroup_relation);
		cancel = (Button) window.findViewById(R.id.custom_alertdialog_cancel);
		ensure = (Button) window.findViewById(R.id.custom_alertdialog_ensure);
	}

	/**
	 * 左边按钮点击事件
	 * 
	 * @param listener
	 */
	public void setLeftOnClick(View.OnClickListener listener) {
		cancel.setOnClickListener(listener);
	}

	public void setOnStateChagedListener(OnStateChagedListener mCallBack) {
		this.mCallBack = mCallBack;
	}

	/**
	 * 右边按钮点击事件
	 * 
	 * @param listener
	 */
	public void setRightOnClick(View.OnClickListener listener) {
		ensure.setOnClickListener(listener);
	}

	/**
	 * 设置标题
	 * 
	 * @param title
	 */
	public void setTitle(String title) {
		if (title.equals("")) {
			this.title.setVisibility(View.GONE);
		} else {
			this.title.setText(title);
		}

	}

	/**
	 * 动态添加radiobutton
	 * 
	 * @param title
	 */

	public void addRelationRadio(List<RelationType> relationTypeList) {
		for (RelationType relationType : relationTypeList) {
			if (!relationType.getTypeCode().equals("owner")) {
				RadioButton radio = new RadioButton(context);
				radio.setText(relationType.getTypeName());
				radio.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
				radio.setTextColor(context.getResources().getColor(
						R.color.nw_bg_black));
				radio.setPadding(100, 0, 0, 0);
				radioGroup_relation.addView(radio);
			}
		}

	}

	/**
	 * 获取被选中的radiobutton对应的relationtypeid
	 * 
	 * @param title
	 */

	public void addRelationRadio(String title) {
		RadioButton radio = new RadioButton(context);
		radio.setText(title);
		radio.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
		radio.setTextColor(context.getResources().getColor(R.color.nw_bg_black));
		radio.setPadding(100, 0, 0, 0);
		radioGroup_relation.addView(radio);
	}

	/**
	 * 设置详情
	 * 
	 * @param detial
	 */
	public void onCheckedChanged(RadioGroup radio1, int id1) {
		// TODO Auto-generated method stub
		if (id1 == radioButton_family.getId()) {
			// 把mRadio1的内容传到mTextView1
			tryNotifyStateChanged();

		} else if (id1 == radioButton_relation.getId()) {
			// 把mRadio2的内容传到mTextView1
			// sexEdit.setText("女");
			tryNotifyStateChanged();

		} else if (id1 == radioButton_rentant.getId()) {
			// 把mRadio2的内容传到mTextView1
			// sexEdit.setText("女");
			tryNotifyStateChanged();

		}
	}

	private void tryNotifyStateChanged() {
		// TODO Auto-generated method stub
		if (mCallBack != null) {
			mCallBack.onStateChanged(str);
		}
	}

	/**
	 * 设置左边按钮背景
	 * 
	 * @param cancelBg
	 */
	public void setCancelBg(int cancelBg) {
		cancel.setBackgroundResource(cancelBg);
	}

	/**
	 * 设置右边按钮背景
	 * 
	 * @param cancelBg
	 */
	public void setEnsureBg(int ensureBg) {
		ensure.setBackgroundResource(ensureBg);
	}

	/**
	 * 设置隐藏左边按钮背景
	 * 
	 * @param cancelBg
	 */
	public void setLeftVisible(int ensureBg) {
		cancel.setVisibility(View.GONE);
	}

	public void show() {
		mDialog.show();
	}

	public void dismiss() {
		mDialog.dismiss();
	}
}
